package org.greyhawk.samonitor.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.greyhawk.samonitor.domain.Advert;
import org.greyhawk.samonitor.domain.Resources;
import org.greyhawk.samonitor.domain.Site;
import org.greyhawk.samonitor.domain.Statute;
import org.greyhawk.samonitor.domain.StatuteClassification;
import org.greyhawk.samonitor.service.FileGenerater;
import org.greyhawk.samonitor.vo.AdvertForm;
import org.greyhawk.samonitor.vo.AdvertStatus;
import org.greyhawk.samonitor.vo.CollectForm;
import org.greyhawk.samonitor.vo.MainClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@RequestMapping("/resource/**")
@Controller
public class ResourcesController {
	
	@Autowired
	private FileGenerater fileGenerater;
		
	
	@RequestMapping
	public ModelAndView list(HttpServletRequest request){
		ModelAndView view = new ModelAndView();
    	Long size = Site.findMainSitesCount();
    	List<Site> sites = Site.findMainSites(0,size.intValue());
		view.addObject("siteList", sites);
		view.addObject("size", size);
		view.setViewName("/resource/list");
		MainClass mainClass = new MainClass();
    	mainClass.collect();
    	request.getSession(true).setAttribute("mainClass", mainClass);
		return view;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(method=RequestMethod.POST)
    public ModelAndView submit(CollectForm collectForm,BindingResult bindingResult,HttpServletRequest request){
		System.out.println("submit:"+collectForm.toString());
		ModelAndView view = new ModelAndView();
    	Long siteId = collectForm.getQuerySiteId();
    	Site site = Site.findSite(siteId);
    	if(collectForm.getStatus().equals(AdvertStatus.N.name())){
    		List<Resources> resourcesList = Resources.findResourcesBySiteAndType(site, collectForm.getResourcesType(), collectForm.getFirstResult(),collectForm.getMaxResult());
    		view.addObject("resourcesList", resourcesList);
    		Long resourcesCount = Resources.findResourcesCountBySiteAndType(site, collectForm.getResourcesType());
    		collectForm.setResCount(resourcesCount);
    	}
    	else{
    		List<Advert> advertList = Advert.findAdvertBySiteAndStatus(collectForm.getResourcesType(), site, collectForm.getAdvertStatus())
    										.setFirstResult(collectForm.getFirstResult()).setMaxResults(collectForm.getMaxResult()).getResultList();
    		view.addObject("advertList", advertList);
    		Long resourcesCount = Advert.findAdvertCountBySiteAndStatus(collectForm.getResourcesType(), site, collectForm.getAdvertStatus());
    		collectForm.setResCount(resourcesCount);
    	}
    	List<StatuteClassification> staClasses = StatuteClassification.findAllStatuteClassification();
    	view.addObject("advertStatus", collectForm.getStatus());
    	view.addObject("staClasses", staClasses);
    	Long size = Site.findMainSitesCount();
    	List<Site> sites = Site.findMainSites(0,size.intValue());
		view.addObject("siteList", sites);
		view.addObject("size", size);
		view.setViewName("/resource/list");
		request.getSession(true).setAttribute("collectForm", collectForm);
		return view;
    }
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/resource/page/{pageId}",method=RequestMethod.GET)
	public ModelAndView page(@PathVariable Integer pageId,HttpServletRequest request){
		CollectForm collectForm = (CollectForm)request.getSession().getAttribute("collectForm");
		System.out.println("page:"+pageId+" form:"+collectForm.toString());
				ModelAndView view = new ModelAndView();
    	Long siteId = collectForm.getQuerySiteId();
    	Site site = Site.findSite(siteId);
    	if(collectForm.getStatus().equals(AdvertStatus.N.name())){
    		List<Resources> resourcesList = Resources.findResourcesBySiteAndType(site, collectForm.getResourcesType(), collectForm.getFirstResult(),collectForm.getMaxResult());
    		view.addObject("resourcesList", resourcesList);
    		Long resourcesCount = Resources.findResourcesCountBySiteAndType(site, collectForm.getResourcesType());
    		collectForm.setResCount(resourcesCount);
    	}
    	else{
    		List<Advert> advertList = Advert.findAdvertBySiteAndStatus(collectForm.getResourcesType(), site, collectForm.getAdvertStatus())
    										.setFirstResult(collectForm.getFirstResult()).setMaxResults(collectForm.getMaxResult()).getResultList();
    		view.addObject("advertList", advertList);
    		Long resourcesCount = Advert.findAdvertCountBySiteAndStatus(collectForm.getResourcesType(), site, collectForm.getAdvertStatus());
    		collectForm.setResCount(resourcesCount);
    	}
    	List<StatuteClassification> staClasses = StatuteClassification.findAllStatuteClassification();
    	view.addObject("advertStatus", collectForm.getStatus());
    	view.addObject("staClasses", staClasses);
    	Long size = Site.findMainSitesCount();
    	List<Site> sites = Site.findMainSites(0,size.intValue());
		view.addObject("siteList", sites);
		view.addObject("size", size);
		view.setViewName("/resource/list");
		if(pageId != 0)
		{
			collectForm.setCurrPage(pageId);
			request.getSession().setAttribute("collectForm", collectForm);
		}
		return view;
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String submitAdvert(AdvertForm advertForm,BindingResult bindingResult){
		System.out.println(advertForm.toString());
		Advert advert = new Advert();
		Resources resources = Resources.findResources(advertForm.getStatResource());
		resources.setIsJudge(true);
		advert.setResources(resources);
		advert.setAdvertStatus(advertForm.getAdStatus());
		if(advertForm.getAdStatus().equals(AdvertStatus.IL)){
			advert.setStatute(Statute.findStatute(advertForm.getStatute()));
			advert.setExplain(advertForm.getExplain());
		}
		advert.merge();
		return "redirect:/resource/list";
	}
	
	
	
	
	
	@RequestMapping("/resource/image/{resourcesId}")
	public void showResources(@PathVariable long resourcesId,HttpServletResponse response){
		Resources resources = Resources.findResources(resourcesId);
		response.setContentType("image/jpeg");
		try {
			//response.getOutputStream().write(resources.getData());
			response.getOutputStream().write(fileGenerater.readResources(resources));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/resource/flash/{resourcesId}")
	public void showFlash(@PathVariable long resourcesId,HttpServletResponse response){
		Resources resources = Resources.findResources(resourcesId);
		response.setContentType("application/x-shockwave-flash");
		try {
			//response.getOutputStream().write(resources.getData());
			response.getOutputStream().write(fileGenerater.readResources(resources));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	@RequestMapping("/resource/staclass/{id}")
	public @ResponseBody List<Statute> showStatutes(@PathVariable Integer id){
		System.out.println("statuteClassId:"+id);
		List<Statute> jsonList = new ArrayList<Statute>();
		System.out.println("jsonList size:"+jsonList.size());
		Set<Statute> statuteSet = StatuteClassification.findStatuteClassificationById(id).getStatutes();
		for (Statute statute : statuteSet) {
			Statute cope = new Statute();
			cope.setId(statute.getId());
			cope.setDetails(statute.getDetails());
			cope.setName(statute.getName());
			jsonList.add(cope);
		}
		System.out.println("jsonList size:"+jsonList.size());
		return jsonList;
	}
	
	@RequestMapping("/resource/statute/{id}")
	public @ResponseBody Statute showDetails(@PathVariable Integer id){
		String details = Statute.findStatute(id).getDetails();
		Statute statute = new Statute();
		statute.setDetails(details);
		System.out.println("statute details:"+details);
		return statute;
	}
	
	

}
